package com.hongxin.nw.netty.down;

import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_15;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

import java.util.Map;

/**
 * @remark A.31 远程升级启动
 * @author: GuoXiaolin
 * @date 2019/6/25 16:09
 * @Version 1.0
 * @Descibe
 */
public class UpgradeReq implements IChannelRequest {
    @SuppressWarnings("unchecked")
    @Override
    public String doPacking(Transfer transfer)  throws Exception{
        Map<String, String>  map = (Map<String, String>) transfer.getObj();
        String code = transfer.getCode();
        Csdna1_15 csdna1_15 = new Csdna1_15()
                .setCo(code)
                .setUpgrade(map);

        //TYPE = 133 , COT = 6
        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_REMOTE_UPGRADE_START.getInt()));
        mm.setSendData(csdna1_15.packing());

//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setCot(TypeIdentif.COT_ACTIVATION.getInt());
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());
//        as.setInfoadr(0);
//        as.setVsq(StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_ACTIVATION.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(), 0,
                StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt());
//        ac.setApduLength(as.getHexData().length()/2);
//        ac.setContarea(TypeIdentif.FORMAT_I.getStr(), StaticValue.U_TEST_V);
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_TEST_V);
        return ac + data;
    }

}
